package com.nehc.nettyserver.common.model;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 消息实体类
 * 
 * @author NEHC
 */
@Data
@NoArgsConstructor
public class Message {
    /**
     * 消息ID
     */
    private String id;
    
    /**
     * 消息类型
     */
    private String type;
    
    /**
     * 发送者ID
     */
    private String senderId;
    
    /**
     * 接收者ID
     */
    private String receiverId;
    
    /**
     * 消息时间戳
     */
    private long timestamp;
    
    /**
     * 消息负载
     */
    private Map<String, Object> payload;
    
    /**
     * 创建一个新消息
     */
    public Message(String type, String senderId, String receiverId, Map<String, Object> payload) {
        this.id = UUID.randomUUID().toString();
        this.type = type;
        this.senderId = senderId;
        this.receiverId = receiverId;
        this.timestamp = System.currentTimeMillis();
        this.payload = payload != null ? payload : new HashMap<>();
    }
    
    /**
     * 创建一个系统消息
     */
    public static Message createSystemMessage(String content) {
        Map<String, Object> payload = new HashMap<>();
        payload.put("content", content);
        
        Message message = new Message();
        message.id = UUID.randomUUID().toString();
        message.type = "SYSTEM";
        message.senderId = "system";
        message.timestamp = System.currentTimeMillis();
        message.payload = payload;
        
        return message;
    }
    
    /**
     * 创建一个心跳消息
     */
    public static Message createHeartbeatMessage() {
        Map<String, Object> payload = new HashMap<>();
        payload.put("timestamp", System.currentTimeMillis());
        
        Message message = new Message();
        message.id = UUID.randomUUID().toString();
        message.type = "HEARTBEAT";
        message.senderId = "system";
        message.timestamp = System.currentTimeMillis();
        message.payload = payload;
        
        return message;
    }
    
    /**
     * 添加负载项
     */
    public void addPayload(String key, Object value) {
        if (this.payload == null) {
            this.payload = new HashMap<>();
        }
        this.payload.put(key, value);
    }
} 